package lanQiaoBei.testQuestions.编程题;

import java.util.Scanner;

/*
*
* 13届蓝桥杯C++ B组第二题
* */
public class _04 {
    public static void main(String[] args) {
        int[]months={30,29,31,30,31,30,31,30,31,30,31,30};
        int year=2022;
        int month=1;
        int day=1;
        int res=0;
        for (int i = 0;i<12;) {
            String date = String.format("%04d%02d%02d", year, month, day);
            if(check(date)){
                res++;
                System.out.println(date);
            }
            if(day==months[i]){
                day=1;
                month++;
                i=month-1;
            }else {
                day++;
            }
        }
        System.out.println(res);
    }

    private static boolean check(String date) {
            if(date.charAt(5)==date.charAt(4)+1&&date.charAt(6)==date.charAt(4)+2){
                return true;
            }
           if(date.charAt(6)==date.charAt(5)+1&&date.charAt(7)==date.charAt(5)+2){
            return true;
           }
           return false;
    }
}

class Main{
    public static void main(String[]args){
        Scanner scanner= new Scanner(System.in);
        int N= scanner.nextInt();
        int ma=scanner.nextInt();
        int[] maArr= new int[8];
//        String[] s= scanner.nextLine().split(" ");
        for(int i=ma-1,j=0;i>=0;i--){
                maArr[j++]=Integer.parseInt(scanner.next("%d "));
        }
        int mb= scanner.nextInt();
        int[]mbArr=new int[8];
//        s=scanner.nextLine().split(" ");
        for(int i=mb-1,j=0;i>=0;i--){
            maArr[j++]=Integer.parseInt(scanner.next("%d "));
        }

        long res=Integer.MAX_VALUE;
        for(int p0=Math.max(Math.max(maArr[0],2),mbArr[0]);p0<=10;p0++)
            for(int p1=Math.max(Math.max(maArr[1],2),mbArr[1]);p1<=10;p1++)
                for(int p2=Math.max(Math.max(maArr[2],2),mbArr[2]);p2<=10;p2++)
                    for(int p3=Math.max(Math.max(maArr[3],2),mbArr[3]);p3<=10;p3++)
                        for(int p4=Math.max(Math.max(maArr[4],2),mbArr[4]);p4<=10;p4++)
                            for(int p5=Math.max(Math.max(maArr[5],2),mbArr[5]);p5<=10;p5++)
                                for(int p6=Math.max(Math.max(maArr[6],2),mbArr[6]);p6<=10;p6++)
                                    for(int p7=Math.max(Math.max(maArr[7],2),mbArr[7]);p7<=10;p7++){
                                        long sum=(maArr[0]-mbArr[0])*1+(maArr[1]-mbArr[1])*p0+(maArr[2]-mbArr[2])*p0*p1+(maArr[3]-mbArr[3])*p0*p1*p2
                                                +(maArr[4]-mbArr[4])*p0*p1*p2*p3+(maArr[5]-mbArr[5])*p0*p1*p2*p3*p4+(maArr[6]-mbArr[6])*p0*p1*p2*p3*p4*p5
                                                +(maArr[7]-mbArr[7])*p0*p1*p2*p3*p4*p5*p6;
                                        res=(long)Math.min(res,sum);
                                    }
        System.out.print(res%1000000007);
    }
}

